package com.lrkj.waitress.service.impl;

import java.util.List;

import com.lrkj.common.constant.FeeConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.framework.utils.NumberUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.property.domain.Community;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.waitress.domain.CustomInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.waitress.mapper.PlaceMapper;
import com.lrkj.waitress.domain.Place;
import com.lrkj.waitress.service.IPlaceService;

/**
 * 地点管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-22
 */
@Service
public class PlaceServiceImpl implements IPlaceService
{
    @Autowired
    private PlaceMapper placeMapper;

    @Autowired
    private ICommunityService communityService;

    /**
     * 查询地点管理
     *
     * @param id 地点管理ID
     * @return 地点管理
     */
    @Override
    public Place selectPlaceById(String id)
    {
        return placeMapper.selectPlaceById(id);
    }

    /**
     * 查询地点管理列表
     *
     * @param place 地点管理
     * @return 地点管理
     */
    @Override
    public List<Place> selectPlaceList(Place place)
    {
        return placeMapper.selectPlaceList(place);
    }

    /**
     * 新增地点管理
     *
     * @param place 地点管理
     * @return 结果
     */
    @Override
    public int insertPlace(Place place)
    {
        place.setId(IdUtils.simpleUUID());
        place.setCreateBy(SecurityUtils.getUsername());
        place.setCreateTime(DateUtils.getNowDate());
        place.setPlaceState("10");
        if (place.getPlaceOdd()==null){
            place.setPlaceOdd(this.getNo(FeeConstants.place_odd));
        }
        this.setInfo(place);
        return placeMapper.insertPlace(place);
    }

    /**
     * 修改地点管理
     *
     * @param place 地点管理
     * @return 结果
     */
    @Override
    public int updatePlace(Place place)
    {
        place.setUpdateBy(SecurityUtils.getUsername());
        place.setUpdateTime(DateUtils.getNowDate());
        this.setInfo(place);
        return placeMapper.updatePlace(place);
    }

    /**
     * 批量删除地点管理
     *
     * @param ids 需要删除的地点管理ID
     * @return 结果
     */
    @Override
    public int deletePlaceByIds(String[] ids)
    {
        return placeMapper.deletePlaceByIds(ids);
    }

    /**
     * 删除地点管理信息
     *
     * @param id 地点管理ID
     * @return 结果
     */
    @Override
    public int deletePlaceById(String id)
    {
        return placeMapper.deletePlaceById(id);
    }

    @Override
    public int updStop(Place place) {
        return placeMapper.updStop(place);
    }

    /**
     * 批量新增地点管理信息
     *
     * @param places 地点管理对象
     */
    public int batchAddData(List<Place> places)
    {
           return placeMapper.batchAddData(places);
    }


    private void setInfo(Place place) {
        Community community = communityService.selectCommunityById(place.getCommunityId());
        place.setDeptId(community.getDeptId());
        place.setAncestors(community.getAncestors() + "," + community.getId());
    }


    public String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }
}
